<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Objects</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="advanced.features.html" title="Chapter 17. Advanced Features">
<link rel="prev" href="advanced.features.streams.html" title="Streams">
<link rel="next" href="advanced.features.static.classes.html" title="Static Classes">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Objects</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="advanced.features.streams.html">Prev</a> </td>
<th width="60%" align="center">Chapter 17. Advanced Features</th>
<td width="20%" align="right"> <a accesskey="n" href="advanced.features.static.classes.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="Objects">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="advanced.features.objects"></a>Objects</h2></div></div></div>
<p>
  Smarty allows access to PHP
  <a class="ulink" href="http://php.net/object" target="_top">objects</a> through the templates.
</p>
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
  When you assign/register objects to templates, be sure that all properties and methods accessed from the template are for presentation purposes only. It is very easy to inject application logic through objects, and this leads to poor designs that are difficult to manage. See the Best Practices section of the Smarty website.
</p>
</div>
<p>
  There are two ways to access them.
</p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc" compact>
<li class="listitem"><p>
 One way is to <a class="link" href="api.register.object.html" title="registerObject()">register objects</a> to
 the template, then use access them via syntax similar to
 <a class="link" href="language.custom.functions.html" title="Chapter 8. Custom Functions">custom functions</a>.
 </p></li>
<li class="listitem"><p>
   The other way is to <a class="link" href="api.assign.html" title="assign()"><code class="varname">assign()</code></a> 
   objects to the templates and access them much like any other assigned variable.
  </p></li>
</ul></div>
<p>
    The first method has a much nicer template syntax. It
	 is also more secure, as a registered object can be restricted to certain
 methods or properties. However,
 <span class="bold"><strong>a registered object cannot be looped over
 or assigned in arrays of objects</strong></span>, etc. The method you choose will be
	 determined by your needs, but use the first method whenever possible to
	 keep template syntax to a minimum.
 </p>
<p>
  If security is enabled, no private methods or functions can be accessed
 	(beginningwith '_'). If a method and property of the same name exist,
 	the method will be used.
	</p>
<p>
 	You can restrict the methods and properties that can be accessed by
 	listing them in an array as the third registration parameter.
	</p>
<p>
	 By default, parameters passed to objects through the templates are passed
  the same way
  <a class="link" href="language.custom.functions.html" title="Chapter 8. Custom Functions">custom functions</a> get them.
  An associative array is passed
	 as the first parameter, and the smarty object as the second. If you want
	 the parameters passed one at a time for each argument like traditional
	 object parameter passing, set the fourth registration parameter to <code class="constant">FALSE</code>.
	</p>
<p>
	 The optional fifth parameter has only effect with
	 <em class="parameter"><code>format</code></em> being <code class="constant">TRUE</code>
	 and contains a list of methods that should be treated as
	 blocks. That means these methods have a closing tag in the
	 template
	 (<code class="literal">{foobar-&gt;meth2}...{/foobar-&gt;meth2}</code>) and
	 the parameters to the methods have the same synopsis as the
  parameters for
  <a class="link" href="plugins.block.functions.html" title="Block Functions">
  <code class="varname">block-function-plugins</code></a>:
 They get the four parameters
	 <em class="parameter"><code>$params</code></em>,
	 <em class="parameter"><code>$content</code></em>,
	 <em class="parameter"><code>$smarty</code></em> and
	 <em class="parameter"><code>&amp;$repeat</code></em> and they also behave like
	 block-function-plugins.
	</p>
<div class="example">
<a name="id459938"></a><p class="title"><b>Example 17.9. Using a registered or assigned object</b></p>
<div class="example-contents">
<pre class="programlisting">

&lt;?php
// the object

class My_Object {
	function meth1($params, $smarty_obj) {
		return 'this is my meth1';
	}
}

$myobj = new My_Object;

// registering the object (will be by reference)
$smarty-&gt;registerObject('foobar',$myobj);

// if we want to restrict access to certain methods or properties, list them
$smarty-&gt;registerObject('foobar',$myobj,array('meth1','meth2','prop1'));

// if you want to use the traditional object parameter format, pass a boolean of false
$smarty-&gt;registerObject('foobar',$myobj,null,false);

// We can also assign objects. assign_by_ref when possible.
$smarty-&gt;assign_by_ref('myobj', $myobj);

$smarty-&gt;display('index.tpl');
?&gt;

  </pre>
<p>
   And here's how to access your objects in <code class="filename">index.tpl</code>:
  </p>
<pre class="programlisting">

{* access our registered object *}
{foobar-&gt;meth1 p1='foo' p2=$bar}

{* you can also assign the output *}
{foobar-&gt;meth1 p1='foo' p2=$bar assign='output'}
the output was {$output}

{* access our assigned object *}
{$myobj-&gt;meth1('foo',$bar)}

  </pre>
</div>
</div>
<br class="example-break"><p>
  See also <a class="link" href="api.register.object.html" title="registerObject()"><code class="varname">registerObject()</code></a>
  and
  <a class="link" href="api.assign.html" title="assign()"><code class="varname">assign()</code></a>.
 </p>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="advanced.features.streams.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="advanced.features.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="advanced.features.static.classes.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Streams </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> Static Classes</td>
</tr>
</table>
</div>
</body>
</html>
